package com.ghcnb.jyt.hosp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ghcnb.jyt.common.result.Result;
import com.ghcnb.jyt.common.utils.MD5;
import com.ghcnb.jyt.hosp.mapper.HospitalSetMapper;
import com.ghcnb.jyt.hosp.service.HospitalSetService;
import com.ghcnb.jyt.model.hosp.HospitalSet;
import com.ghcnb.jyt.vo.hosp.HospitalSetQueryVo;
import com.mysql.jdbc.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RequestMapping("/admin/hosp/hospitaiSet")
@RestController
//允许跨域访问
@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;
    // 具体方法
    // 1 查询
    @ApiOperation(value = "获取所有医院信息")
    @GetMapping("findAll")
    public Result findAllHospitalSet(){
        //调用service的方法
        //http://localhost:8201/admin/hosp/hospitaiSet/findAll
        List<HospitalSet> list=hospitalSetService.list();
        return Result.ok(list);
    }
    // 2 删除
    @ApiOperation(value = "逻辑删除")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id){
        boolean flag=hospitalSetService.removeById(id);
        if(flag)return Result.ok();
        return Result.fail();
    }

    //3 条件查询带分页
    @ApiOperation(value = "条件查询带分页")
    @PostMapping ("findPage/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        // 创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page=new Page<>(current,limit);
        //构造条件
        QueryWrapper<HospitalSet> wrapper=new QueryWrapper<>();
        if (!StringUtils.isNullOrEmpty(hospitalSetQueryVo.getHosname()))
            wrapper.like("hosname",hospitalSetQueryVo.getHosname());
        if (!StringUtils.isNullOrEmpty(hospitalSetQueryVo.getHoscode()))
            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        //调用方法实现分页查询
        Page<HospitalSet> hospitalSet = hospitalSetService.page(page, wrapper);
        return Result.ok(hospitalSet);

    }

    //4 添加医院设置
    @ApiOperation(value = "添加医院设置")
    @PostMapping("saveHospialSet")
    public Result saveHospialSet(@RequestBody HospitalSet hospitalSet){
        //设置状态 1可用 0 不可用
        hospitalSet.setStatus(1);
        //签名密钥
        Random random=new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        //调用service
        boolean save =hospitalSetService.save(hospitalSet);
        if(save)return Result.ok();
        return Result.fail();
    }

    //5 根据id获取医院数据
    @ApiOperation(value = "根据id获取医院数据")
    @GetMapping("GetHostSetById/{id}")
    public Result getHostSetById(@PathVariable long id){
        return Result.ok(hospitalSetService.getById(id));
    }

    //6修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PostMapping("updateHospialSet")
    public Result UpdateHostSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag)return Result.ok();
        return Result.fail();
    }
    //7批量删除医院设置 [值，值，。。。]
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHostSet(@RequestBody List<Long> ids) {
        return Result.ok(hospitalSetService.removeByIds(ids));
    }

    //8医院设置的锁定与解锁
    @ApiOperation(value = "医院设置的锁定与解锁")
    @PostMapping("LockHospialSet/{id}/{status}")
    public Result LockAndUnlockHostSet(@PathVariable Long id,
                                       @PathVariable Integer status){
        HospitalSet hospitalSet=hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }
    //9发送签名密钥
    @ApiOperation(value = "发送签名密钥")
    @PostMapping("SendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hosname = hospitalSet.getHosname();
        //发送短信


        return Result.ok(hospitalSet);

    }
}
